﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using DM_Article.Model;

namespace DM_Article.Bll
{
    public class articlebll
    {
        public static int add(SqlConnection conn, article model)
        {
            SqlCommand cmd = new SqlCommand("insert into article(title,keyword,content,bigclassid,smallclassid,origin,author,addtime)values('" + model.title + "','" + model.keyword + "','" + model.content + "','" + model.bigclassid + "','" + model.smallclassid + "','" + model.origin + "','" + model.author + "','" + model.addtime + "')", conn);
            object obj = cmd.ExecuteNonQuery();
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public static int update(SqlConnection conn, article model)
        {
            SqlCommand cmd = new SqlCommand("update article set title='" + model.title + "',keyword ='" + model.keyword + "',content='" + model.content + "',bigclassid='" + model.bigclassid + "',smallclassid='" + model.smallclassid + "',origin='" + model.origin + "',author='" + model.author + "',addtime='" + model.addtime + "' where id='" + model.id + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static int delete(SqlConnection conn, string id)
        {
            SqlCommand cmd = new SqlCommand("delete article where id='" + id + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static int deleteBySmallclass(SqlConnection conn, string smallclassid)
        {
            SqlCommand cmd = new SqlCommand("delete article where smallclassid='" + smallclassid + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static int deleteByBigclass(SqlConnection conn, string bigclassid)
        {
            SqlCommand cmd = new SqlCommand("delete article where bigclassid='" + bigclassid + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static article getArticleDetail(SqlConnection conn, string id)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [title],[keyword],[content],[bigclassid],[smallclassid],[origin],[author],[addtime],[hits]  from article where id='" + id + "'", conn);
            DataTable dt = new DataTable();
            article model =new article();
            model.id = Convert.ToInt32(id);
            da.Fill(dt);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["title"].ToString() != "")
                {
                    model.title = dt.Rows[0]["title"].ToString();
                }
                if (dt.Rows[0]["keyword"].ToString() != "")
                {
                    model.keyword = dt.Rows[0]["keyword"].ToString();
                }
                if (dt.Rows[0]["content"].ToString() != "")
                {
                    model.content = dt.Rows[0]["content"].ToString();
                }
                if (dt.Rows[0]["bigclassid"].ToString() != "")
                {
                    model.bigclassid = Convert.ToInt32(dt.Rows[0]["bigclassid"].ToString());
                }
                if (dt.Rows[0]["smallclassid"].ToString() != "")
                {
                    model.smallclassid = Convert.ToInt32(dt.Rows[0]["smallclassid"].ToString());
                }
                if (dt.Rows[0]["origin"].ToString() != "")
                {
                    model.origin = dt.Rows[0]["origin"].ToString();
                }
                if (dt.Rows[0]["author"].ToString() != "")
                {
                    model.author = dt.Rows[0]["author"].ToString();
                }
                if (dt.Rows[0]["addtime"].ToString() != "")
                {
                    model.addtime = dt.Rows[0]["addtime"].ToString();
                }
                if (dt.Rows[0]["hits"].ToString() != "")
                {
                    model.hits = Convert.ToInt32(dt.Rows[0]["hits"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }

        }

        public static DataTable getArticleTable(SqlConnection conn)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [id],[title],[content],[bigclassid],[smallclassid],[origin],[author],[addtime],[hits] from article order by addtime DESC", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable getArticleByHitsTable(SqlConnection conn, int top)
        {
            SqlDataAdapter da = new SqlDataAdapter("select top "+ top.ToString() +"[hits],[id],[title],[bigclassid],[smallclassid],[origin],[author],[addtime] from article order by hits DESC", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conn">database connectting string</param>
        /// <param name="smallclassid">small class id</param>
        /// <param name="top">get top data item from database</param>
        /// <returns>return type is 'DataSet'</returns>
        public static DataSet getArticleBySmallclassidDatasetTop(SqlConnection conn, int smallclassid, int top)
        {
            SqlDataAdapter da = new SqlDataAdapter("select TOP " + top.ToString() +" [id],[title],[addtime] from article where smallclassid=" + smallclassid.ToString() + " order by addtime DESC", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
    }
}
